package com.zlh.hdfsfilesys.controller;

import com.zlh.hdfsfilesys.entity.User;
import com.zlh.hdfsfilesys.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Random;

@Controller
public class LoginController {

    @Autowired
    private UserMapper userMapper;

    /**
     * 响应默认页面到login页面
     * @param model
     * @return
     */
    @RequestMapping("/")
    public String index(Model model){
        User user = new User(0,"","","");
        model.addAttribute(user);
        return "login";
    }

    /**
     * 登录响应，接收http请求然后在数据库中搜索并返回结果
     * @param name 用户输入的用户名 从http请求中获取
     * @param password 用户输入的密码 从http请求中获取
     * @return
     */
    @RequestMapping("/login")
    public String login(String name, String password, Model model){
        List<User> userList = userMapper.selUserLogin(name, password);
        if (userList.size() == 0){
            return "failed";
        }else if (userList.size()>1){
            return "failed";

        }
        else {
            return "wait";
        }

    }

    /**
     * 响应注册请求，调用mybitis-plus向数据库插入记录
     * @param name 用户输入的用户名 从http请求中获取
     * @param password 用户输入的密码 从http请求中获取
     * @param tel 用户输入的电话 从http请求中获取
     * @return 返回到主页面
     */
    @RequestMapping("/signup")
    public String signup(String name,String password,String tel,Model model){
        int id =(int) (Math.random()*9000+1000);
        User user =new User(id,name,password,tel);
        if (name.isEmpty() || password.isEmpty() || tel.isEmpty()){
            return "failed";
        }
        userMapper.insert(user);
        System.out.println(name+password+tel);
        return "wait";

    }
}
